﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace NExplorer
{
	public static class Extenders
	{
		/// <summary>
		/// Indicates whether <paramref name="collection"/> contains any of the members in <paramref name="toFind"/>.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="collection"></param>
		/// <param name="toFind"></param>
		/// <returns></returns>
		public static bool ContainsAny<T>(this ICollection<T> collection, ICollection<T> toFind)
		{
			bool found = false;

			foreach (T item in toFind)
			{
				if (collection.Contains(item))
				{
					found = true;
					break;
				}
			}

			return found;
		}
		
		public static bool ContainsAll<T>(this ICollection<T> collection, ICollection<T> toFind)
		{
			bool foundAll;
	
			if (toFind.Count == 0)
			{
				foundAll = false;
			}
			else
			{
				foundAll = true;

				foreach (T item in toFind)
				{
					if (!collection.Contains(item))
					{
						foundAll = false;
						break;
					}
				}
				
			}
			return foundAll;
		}

		/// <summary>
		/// Returns all of the items in <paramref name="collection"/> that also exist in <paramref name="toFind"/>.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="collection"></param>
		/// <param name="toFind"></param>
		/// <returns></returns>
		public static IList<T> GetMatches<T>(this ICollection<T> collection, ICollection<T> toFind)
		{
			List<T> matches = new List<T>();

			foreach (T item in collection)
			{
				if (toFind.Contains(item))
				{
					matches.Add(item);
				}
			}

			return matches;
		}

		public static void ForEach<T>(this ICollection<T> collection, Action<T> action)
		{
			foreach (T item in collection)
			{
				action(item);
			}
		}

		public static T[] ToArray<T>(this ICollection<T> collection)
		{
			T[] array = new T[collection.Count];
			int index = 0;

			foreach (T item in collection)
			{
				array[index++] = item;
			}

			return array;
		}

		public static K[] ToArray<T,K>(this ICollection<T> collection, Converter<T,K> converter)
		{
			K[] array = new K[collection.Count];
			int index = 0;

			foreach (T item in collection)
			{
				array[index++] = converter(item);
			}

			return array;
		}

		public static T[] ToArray<T>(this ICollection collection)
		{
			T[] array = new T[collection.Count];
			int index = 0;

			foreach (T item in collection)
			{
				array[index++] = item;
			}

			return array;
		}

		public static T[] ToArray<T>(this T obj)
		{
			return new T[] { obj };
		}

		public static string[] RemoveEmptyElements(this string[] array)
		{
			List<string> list = new List<string>();			

			foreach (string s in array)
			{
				if (!string.IsNullOrEmpty(s))
				{
					list.Add(s);
				}
			}

			return list.ToArray();
		}
	}
}
